var mongoose = require('mongoose');
const add = async (params) => {
    return await mongoose.model('outlet').create(params);

}
const update = async (_id, up) => {
    return await mongoose.model('outlet').update(_id, up);
}
const remove = async (_id) => {
    return await mongoose.model('outlet').remove(_id);
}
const get = async (params) => {
    const result = {};
    const outletModel = mongoose.model('outlet');
    if (params.type && params.value) {
        let { type, value, curPage, eachPage } = params;
        let obj = {};
        obj[type] = value;
        const outletModel = mongoose.model('outlet');
        result.total = await outletModel
            .find(obj)
            .count()
            .exec();
        result.rows = await outletModel
            .find(obj)
            .skip((curPage - 1) * eachPage)
            .limit(eachPage)
            .sort({
                _id: -1
            })
            .exec();
        result.curPage = ~~curPage;
        result.eachPage = ~~eachPage;
        result.maxPage = Math.ceil(result.total / eachPage);
        if (result.rows.length>0 ) {
            return result;
        }else{
            return{msg:"暂无数据"}
        }
    } else {
        let {curPage, eachPage } = params;
        result.total = await outletModel.countDocuments();
        result.rows = await outletModel
            .find()
            .skip((curPage - 1) * eachPage)
            .limit(eachPage)
            .sort({
                _id: -1
            })
            .exec();
        result.curPage = ~~curPage;
        result.eachPage = ~~eachPage;
        result.maxPage = Math.ceil(result.total / eachPage);
        if (result.rows.length>0 ) {
            return result;
        }else{
            return{msg:"暂无数据"}
        }
    }

}
// const search = async (type, value, curPage, eachPage) => {
//     let result = {};
//     let obj = {};
//     obj[type] = value;
//     const outletModel = mongoose.model('outlet');
//     result.total = await outletModel
//         .find(obj)
//         .count()
//         .exec();
//     result.rows = await outletModel
//         .find(obj)
//         .skip((curPage - 1) * eachPage)
//         .limit(eachPage)
//         .sort({
//             _id: -1
//         })
//         .exec();
//     result.curPage = curPage;
//     result.eachPage = eachPage;
//     result.maxPage = Math.ceil(result.total / eachPage);
//     return result;
// }
const upstate = async (_id, state) => {
    return await mongoose.model('outlet').update(_id, state);
}
const getone = async (_id) => {
    return await mongoose.model('outlet').find(_id);
}
const getWait = async (state) => {
    return await mongoose.model('outlet').find({state});
}
const link = async (params) => {
    let {_id,adminName}=params;
     await mongoose.model('admin').find({adminName}).update({outletId:_id});
}
const adminpos = async (adminName) => {
    let res=await mongoose.model('admin').find({adminName});
    if (res[0].outletId) {
        
        return await mongoose.model('admin').find({adminName}).populate({path:'outletId'}).exec();
    }else{
        return  await mongoose.model('admin').find({adminName});
    }
}
module.exports = { add, update, remove, get, upstate,getone,getWait ,link,adminpos};